package class31;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class SubArrayMaxSum {

    public static int maxSum(int[] arr) {
        if (arr == null || arr.length == 0) {
            return 0;
        }
        int max = Integer.MIN_VALUE;
        int cur = 0;
        for (int i = 0; i < arr.length; i++) {
            cur += arr[i];
            max = Math.max(max, cur);
            cur = cur < 0 ? 0 : cur;
        }
        return max;
    }

    public static void printArray(int[] arr){
        for (int i = 0;i!=arr.length;i++){
            System.out.print(arr[i] +" ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,-3,4,2,-2,1,1,5,-9};
        printArray(arr);
        System.out.println(maxSum(arr));
    }
}
